package cn.gson.school.controller.xitong;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.gson.school.dao.xitong.DeptSerchDao;
import cn.gson.school.entity.xitong.DeptEntity;
import cn.gson.school.mappers.xitong.DeptSerchMapper;
import lombok.extern.log4j.Log4j;

@Log4j
@Controller
@RequestMapping("/dept")
public class DeptSerchController {
	@Autowired
	private DeptSerchMapper deptSerchMapper;
	@Autowired
	private DeptSerchDao deptSerchDao;

	@RequestMapping("list")
	@ResponseBody
	public Map<String, Object> index(DeptEntity dept, @RequestParam(name = "page", defaultValue = "1") int pageNum,
			@RequestParam(name = "rows", defaultValue = "20") int pageSize) {
		// 查询数据
		PageHelper.startPage(pageNum, pageSize);
		List<DeptEntity> pageData = deptSerchMapper.deptlist(dept);
		PageInfo<DeptEntity> info = new PageInfo<>(pageData);
		// 组装返回结果
		Map<String, Object> obj = new HashMap<String, Object>();
		obj.put("total", info.getTotal());
		obj.put("rows", info.getList());
		return obj;

	}

	@GetMapping("delete/{id}")
	@ResponseBody
	@Transactional
	public Map<String, Object> delete(@PathVariable("id") Long id) {
		Map<String, Object> obj = new HashMap<String, Object>();
		obj.put("success", false);
		DeptEntity dept;
		if (id != null && (dept = deptSerchDao.findOne(id)) != null) {
			deptSerchDao.delete(dept);
			obj.put("success", true);
		} else {
			obj.put("message", "此用户不存在！");
		}
		return obj;
	}

	@GetMapping({ "xt-add-dept", "xt-add-dept/{id}" })
	public String form(@PathVariable(name = "id", required = false) Long id, Model model) {
		if (id != null) {
			DeptEntity dept = deptSerchDao.findOne(id);
			if (dept != null) {
				// 如果是编辑，则把原有用户信息传回表单页面
				
				model.addAttribute("dept", dept);
			}
		}
		return "xitong/xt-add-dept";
	}

	@PostMapping("save")
	@ResponseBody
	@Transactional
	public JSONObject save(@Valid DeptEntity dept, BindingResult br) {
		System.out.println(dept);
		System.out.println("你进来了码？");
		JSONObject obj = new JSONObject();
		obj.put("success", false);
		if (br.hasErrors()) {
			obj.put("message", "信息校验失败！");
			obj.put("erros", br.getFieldErrors());
		} else {
			
			dept.setCreateTime(new Date());
			deptSerchDao.save(dept);
			obj.put("success", true);
		}
		return obj;

	}

}
